package com.crecheng.shop.Manage;

import com.crecheng.shop.DAO.ItemData;
import com.crecheng.shop.util.ConSQL;
import com.crecheng.shop.util.ListUtil;

import java.util.ArrayList;
import java.util.List;

public final class ItemManage {
    public static List<ItemData> getAll(){
        var d=ConSQL.select(ItemData.class);
        d.forEach(ItemData::init);
        return d;
    }

    public static List<ItemData> get(String other){
        var d=ConSQL.select(ItemData.class,other);
        d.forEach(ItemData::init);
        return d;
    }

    public static List<ItemData> get(int offset,int num){
        if(offset<0||num<0)
            return new ArrayList<>();
        var d=ConSQL.select(ItemData.class,offset,num);
        d.forEach(ItemData::init);
        return d;
    }

    public static ItemData get(int id){
        return ListUtil.first(get("id="+id));
    }

    public static boolean add(ItemData data){
        return ConSQL.insert(ItemData.class,data)>0;
    }

    public static boolean del(int id){
        return ConSQL.delete(ItemData.class,"id="+id)>0;
    }

    public static boolean update(ItemData data){
        return ConSQL.update(ItemData.class,data.key(),data)>0;
    }
}
